<!-- 新增与编辑门店 -->
<template>
  <el-dialog :title="modalData.title" :visible.sync="dialog.show" center width="530px" height="400px" custom-class="mydialog" append-to-body @close="handleClose">

    <div class="dialog-content">
      <el-form ref="formsku" :model="form" :rules="rules" label-width="150px" >

        <el-form-item label="品牌名称：" prop="brandName">
          <el-input v-model="form.brandName" maxlength="50" />
        </el-form-item>

      </el-form>
      <div class="text-center mt-30">
        <el-button class="greenbt bt-wd" type="primary" @click="saveFun('formsku')">保存</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>

import {
  post
} from '@/api/utils'
export default{
  props: {
    modalStaticData: {
      type: Object,
      default: () => {}
    },
    // 父组件调用时传进来的参数
    modalData: {
      type: Object,
      title: '',
      default: () => {}
    }

  },
  data() {
    return {
      dialog: {
        show: true
      },
      form: {
        brandName: ''
      },
      rules: {
        brandName: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' },
          { max: 50, message: '长度在 50 个字符内', trigger: 'blur' }
        ]
      }

    }
  },
  created: function() {
    if (this.modalData.lianbaoBrand) {
      var row = this.modalData.lianbaoBrand
      this.form.brandId = row.brandId
      this.form.brandName = row.brandName

      this.form = Object.assign({}, this.form, {})
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    saveFun(formName) {
      const self = this

      self.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(valid)
          var form = this.form

          var data = { 'brandName': form.brandName, 'brandId': form.brandId

          }

          var url = '/webpoint/brand/add'
          if (data.brandId && data.brandId > 0) {
            url = '/webpoint/brand/update'
          }

          post('/wxh-site-rest/rest/', url, data).then(response => {
            if (response.data.status === 'success') {
              console.log(response)
              this.$message({
                message: '编辑品牌成功'
              })
              this.modalStaticData.closeChildAndRefresh()
            } else {
              this.$message({
                message: response.data.messge,
                type: 'warning'
              })
            }
          }).catch(error => {
            console.log(error)
            this.$message({
              message: '数据加载失败',
              type: 'warning'
            })
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
